GNU Emacs Lisp Reference Manual Learning Notes
Notes recorded during learning Emacs Lisp Reference Manual, I did not finish the whole book, only 12 chapters.
Introduction
nil and t
nil- symbol with name "nil"
- logical truth value
false - empty list
t- truth value t
- symbol "t"
booleanpobj return non-=nil= if obj is t or nil, otherwise not
version info
emacs-version&optional here functionemacs-build-timevariableemacs-versionvariableemacs-major-versionvariableemacs-minor-versionvariable
Data Types
one object can have only one primitive type, but may can belong to several other types primitive types are: integer, float, cons, symbol, string, vector, hash-table, subr, byte-code function
character type
the read syntax of basic char is ?<X>, <X> stands for the character to be represented, so ?A stands for 'A'
equality predicates
-
eqobj1 obj2 - return
tif obj1 and obj2 are the same object, otherwise returnnilintegers with same value are considered to be the same object, so(eq 1 1)will returntexception:
(eq "abc" "abc")will returnnil, but(eq "" "")will returntbecause the empty string is only stored one copy as expected,(eq '(1 2 3) '(1 2 3))will returnnil -
equalobj1 obj2 - return
tif obj1 and obj2 have equal components, otherwise returnnilunlike
eq,equalwill look into arguments to check if there contents are the same, if obj1 and obj2 areeq, they must beequal. so, as expected,(equal '(1 2 3) '(1 2 3))and(equal "abc" "abc")will both returnt
Numbers
two types
integerfloat
type predicates functions
floatpobjectintegerpobjectnumberpobjectnatnumpobject : returnstif object is a natural number, such as 0, 1, 2…zeropobject
comparison functions
maxnum1 &rest nums return the maximum valueminnum1 &rest nums return the minimun value
conversion functions
floatnumber convert the number to float typetruncatenumber &optional divisor convert the number to integer by rounding towards zerofloornumber &optional divisor convert the number to integer by rounding towards negative infinityceilingnumber &optional divisor convert the number to integer by rounding towards positive infinityroundnumber &optional divisor convert the number to integer by rounding towards nearest integer
random function
random&optional limit returns a pseudo-random integerif limit is a positive integer, the value is chosen in [0, limit), and of course only choose integer
if limit is
t, it will choose a new seed, otherwise Emacs will always use the same seed, so the returned pseudo-random integer sequences are always the same
Strings and Characters
predicates functions
stringpobjectstring-or-null-pobjectchar-or-string-pobject
useful functions
substring: to get substringconcat: to concatenate strings togethersplit-string: split string into several stringsstring=/string-equal: judge the equality of two stringsstring-prefix-p: check if a string is a prefix of anotherdowncase/upcase: change a string or a character to opposite case
Lists
cons cells
a cons cell has two slots, the first is called CAR and the second is called CDR, and it may looks like the structure below:
struct cons_cell { void * CAR; void * CDR; };
so the two slots can hold any values, and lists are built up from cons cell, which stores object pointer in CAR slot, and stores the nexe node pointer in CDR slot
element accessing functions
car/cdrpopnth/nthcdrlast
building functions
conslistmake-listappendreversenumber-sequence
modification functions
pushadd-to-listsetcar/setcdrnconc&rest lists : return a list containing all the elements of lists, last CDR of each given list is set to point to next listmemqobject list : test if object is in list, if is in, return the sublist from the position object occurs the first timedelqobject list : delete all occurrences of object in list, note that it useseqto check if two objects are equal, same asmemqremqobject list : returns a list copy with all elementseqto object removedmemberobject list : likememq, only difference is it usesequalto check the equality of two objectsdeleteobject sequence : likedelq, but usesequalfor comparisonremoveobject sequence : likedelete, but it always returns a copy of processed sequence, no matter sequence is a list, vector, or a string
association lists
alist for short, it is a list consists of cons cells, that is to say, every element of alist is a cons cell, the CAR of the cons cell stores key while the CDR stores value
assockey alist : returns the first occurrence of key in alist, usesequalfor comparison, note that it returns the whole cons cell, not only the CDRrassocvalue alist : likeassoc, but uses CDR for comparison,assocuses CARassqkey alist : likeassoc, but useseqfor comparisonrassqvalue alist : …assq-delete-all/rassq-delete-all: …
Sequences, Arrays and Vectors
sequence: list, array array: vector, string, char-table, bool-vector
sequence functions
sequenceplengtheltsequence index : returns the element of sequence at index
array functions
arrayparefarray index : returns element of array at indexasetarray index object : set object to the index element of arrayfillarrayarray object : fills array with object
vector functions
vectorpvector&rest objects : creates a vector with objectsmake-vectorlength object : makes a vector has length and filled by objectvconcat&test sequences : returns a new vector containing all elements in sequences
char-table, bool-vector (skipped)
not so useful, so skip them
Hash Tables (skipped)
not so useful, so skip it
Symbols
symbol components
each symbol has four components:
- print name: the name of the symbol
- value: symbol's current value as a variable
- function: symbol's function definition, can also hold a symbol, a keymap, or a keyboard macro
- property list: symbol's property list
create and intern symbols
symbols are stored in a vector called obarray, symbol name and symbol is with a one-to-one mapping relationship interning a symbol means hash it and put it into obarray, or find it from obarray, so there will never be two symbols with same name in one obarray, or two names pointing to one symbol.
*intern* : find a symbol from obarray, or create a new symbol and put it into obarray. so the finding action is also a interning operation
of course there are uninterned symbols, it means they are not put into any obarray, they may exist in some other object or as variable values
functions
symbol-namemake-symbolinternname &optional obarray : returns the interned symbol with name name, creates a new one and puts it into obarray if no symbol foundintern-softname &optional obarray : returns the already interned symbol with name name, returnsnilif not foundmapatomsfunction &optional obarray : calls function once with each symbol in obarray, then returnsnil
property list
property list (so called plist) is like association list (alist), it is also key-value pair data structure, but it often uses symbol as the key, so a plist may always have unique keys, while alist may not.
plist functions
symbol-plistsymbolsetplistsymbol plistgetsymbol propertyputsymbol property valueplist-getplist propertyplist-putplist property valueplist-memberplist property
Evaluation
a Lisp object that is intended for evaluation is called a form or expression, or S-expression any Lisp object can be evaluated, but in practice only numbers, string, symbols, lists are evaluated very often
kinds of forms
how Emacs evaluates a form depends on its data type: symbols, lists, and "all other data types"
- self-evaluating forms: "all other data types"
the result of self-evaluating is the object itself, so 25 will be evaluated to 25, "abc" will be evaluated to "abc"
- symbol forms: will be evaluated as a variable, so its value field will be returned
- list forms: the evaluating way of a list is determined by its first element, if first element is:
- a symbol: the symbol's function field will be obtained and replace the symbol with that function
- a function: evaluates the remaining elements of the list first, and then call the function with these evaluated remaining elements as parameters
- a macro: evaluates the macro with the remaining elements as parameters without evaluating the remaining elements
- special forms: such as if, and, evaluation of elements in the list depends on the special forms' syntax requirements
Control Structures (skipped)
things about special forms for flow control, such as if, progn, cond, while, so skip it
Variables
local variables
letbindings… forms…let*bindings… forms… : likelet, difference is a local variable binding will take effect immediately before next local variable binding takes placemakeunboundsymbol : cancel the symbol's binding to a valueboundpvariable : to judge if the variable is bound, returnstif its value is not void
global variables
global variables can be defined by defvar and defconst, they do not have actually difference, defconst is intent to inform human readers that the variable may should not be changed, but actually its value can be changed
Functions
function types:
- lambda expression
- primitive : written in C
- special form : like
if,and,while - macro
- command : (includes keyboard macros)
- closure
- byte-code function
- autoload object
mapping functions
mapcarfunction sequence : applies function to each element in sequence in turn, return a list of resultsmapcfunction sequence : likemapcar, but it is used for side effects, return value will be ignoredmapconcatfunction sequence separator : likemapcar, but function's result must be string, and the strings will be concatenated as return value